package com.ksd.modules.rbac.roles.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.modules.rbac.roles.entity.SysRoles;
import com.ksd.modules.rbac.roles.po.SysRolesSavePO;
import com.ksd.modules.rbac.roles.po.SysRolesSearchPO;
import com.ksd.modules.rbac.roles.service.ISysRolesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@RestController
public class SysRolesController  extends BaseController {

    @Autowired
    private ISysRolesService sysRolesService;

    /**
     * 查询首页有角色接口
     *
     * @return
     */
    @PostMapping("/sysroles/list")
    public R<List<SysRoles>> findSysRoles() {
        return R.data(sysRolesService.findSysRoles());
    }

    /**
     * 查询首页有角色接口
     *
     * @param adminSysRolesSearchPO
     * @return
     */
    @PostMapping("/sysroles/page")
    public R<Page<SysRoles>> findSysRolesPage(@Validated @RequestBody SysRolesSearchPO adminSysRolesSearchPO) {
        return R.data(sysRolesService.findSysRolesPage(adminSysRolesSearchPO));
    }

    /**
     * 保存和更新
     *
     * @param adminSysRolesSavePO
     * @return
     */
    @PostMapping("/sysroles/saveupdate")
    @ApiLog("角色保存和更新")
    public R<Boolean> saveUpdateSysRoles(@Validated @RequestBody SysRolesSavePO adminSysRolesSavePO) {
        return R.data(sysRolesService.saveUpdateSysRoles(adminSysRolesSavePO));
    }

    /**
     * 根据id查询菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/sysroles/get/{id}")
    public R<SysRoles> getSysRolesById(@PathVariable("id") Long id) {
        return R.data(sysRolesService.getSysRolesById(id));
    }

    /**
     * 根据id删除角色
     *
     * @param id
     * @return
     */
    @PostMapping("/sysroles/del/{id}")
    @ApiLog("根据id删除角色")
    public R<Boolean> delSysRolesById(@PathVariable("id") Long id) {
        return R.data(sysRolesService.deleteSysRolesById(id));
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/sysroles/status/{id}/{field}/{value}")
    @ApiLog("广告商修改状态")
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(sysRolesService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(sysRolesService
                    .updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /*
     * 查询角色的菜单信息
     */
    @PostMapping("/sysroles/roles/change")
    public R<Map<String, Object>> changeRole(@RequestParam Long roleId){
        return R.data(sysRolesService.findMenusPermissionByRoleId(roleId));
    }
}
